package hkust.ee.fyp.tool;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Log;

public class FacebookPhoto extends Thread {

	static final String TAG = "FacebookPhoto";
	String id = null;
	String photoUrl = null;
	String thumbUrl = null;
	String path = null;
	boolean isFinish = false;

	public FacebookPhoto(String path, String id, String photoUrl,
			String thumbUrl) {
		this.path = path;
		this.id = id;
		this.photoUrl = photoUrl;
		this.thumbUrl = thumbUrl;
	}

	@Override
	public void run() {
		try {
			URL aryURI = new URL(photoUrl);
			Log.d(TAG,photoUrl);
			HttpURLConnection conn = (HttpURLConnection) aryURI	.openConnection();
			conn.setRequestMethod("GET");
			conn.setDoOutput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			File file = new File(path + id + ".jpg");
			if(!file.exists()){
				file.createNewFile();
				FileOutputStream stream = new FileOutputStream(file);
				byte[] buffer = new byte[128];
				int len1 = 0;
				while ((len1 = is.read(buffer)) != -1) {
					stream.write(buffer, 0, len1);
				}
				stream.flush();
				stream.close();
				is.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		// try {
		// URL aryURI = new URL(thumbnail);
		// HttpURLConnection conn = (HttpURLConnection)
		// aryURI.openConnection();
		// conn.setRequestMethod("GET");
		// conn.setDoOutput(true);
		// conn.connect();
		// InputStream is = conn.getInputStream();
		//
		// FileOutputStream stream = new FileOutputStream(albumPath + id +
		// ".jpg");
		// byte[] buffer = new byte[128];
		// int len1 = 0;
		// while ((len1 = is.read(buffer)) != -1) {
		// stream.write(buffer, 0, len1);
		// }
		// stream.flush();
		// stream.close();
		// is.close();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		isFinish = true;
	}
	
	public boolean isFinsih(){
		return isFinish;
	}
}
